package com.ewzj.bigdata.mvp.ui.adapter

import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.loadImgwithloading
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.model.rspbean.CollectionBean
import com.ewzj.bigdata.mvp.model.rspbean.HomeBean
import com.ewzj.bigdata.mvp.ui.activity.HomeWebActivity
import com.ewzj.bigdata.mvp.ui.activity.NewsWebActivity
import com.ewzj.bigdata.mvp.ui.activity.VideoWebActivity
import com.mcxtzhang.swipemenulib.SwipeMenuLayout
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagFlowLayout

/**
 * author:zhangjing
 * 作用:
 * return:
 */

class CollectionAdapter : BaseMultiItemQuickAdapter<CollectionBean, BaseViewHolder> {
    ////表id 1 房源 2 资讯 4 数据 6 视频
    var cancelListener: CancelListener? = null
    private val str = "/"
    private var type: Int=0

    constructor(data: List<CollectionBean>) : super(data) {
        //二手房
        addItemType(2, R.layout.item_collectinfo2)
        //        租房
        addItemType(3, R.layout.item_collectinfo3)
        //        新房
        addItemType(1, R.layout.item_collectinfo1)
        //        其他
        addItemType(0, R.layout.item_collectinfo4)
    }

    constructor(data: List<CollectionBean>, type: Int) : super(data) {
        //二手房
        addItemType(2, R.layout.item_collectinfo2)
        //        租房
        addItemType(3, R.layout.item_collectinfo3)
        //        新房
        addItemType(1, R.layout.item_collectinfo1)
        //        其他
        addItemType(0, R.layout.item_collectinfo4)
        this.type = type
    }

    override fun convert(baseViewHolder: BaseViewHolder, collectionBean: CollectionBean) {
        //        baseViewHolder.setText(R.id.tv_time,collectionBean.getCreate_time());
        if (type == 1) {
            val swipeMenuLayout = baseViewHolder.getView<SwipeMenuLayout>(R.id.swipeMenuLayout)
            swipeMenuLayout.isSwipeEnable = false
        }
        baseViewHolder.getView<View>(R.id.iv_cancelsub).setOnClickListener {
            if (cancelListener != null) {
                cancelListener!!.cancelsub(collectionBean)
            }
        }
        baseViewHolder.getView<View>(R.id.ll_content).setOnClickListener {
            when (baseViewHolder.itemViewType) {
                1, 2, 3 -> HomeWebActivity.startHomeActivity(mContext, collectionBean.collect2hoursebean())
                0 -> if (!TextUtils.isEmpty(collectionBean.json_content.video)) {
                        AppKey.mchaneObject=collectionBean
                        VideoWebActivity.startVideoWebView(mContext,collectionBean.collect2Videobean())
                } else {
                        AppKey.mchaneObject=collectionBean
                        NewsWebActivity.startInfoActivity(mContext,collectionBean.collect2Infobean())
                }
            }
        }
        when (baseViewHolder.itemViewType) {
            1 -> {
                baseViewHolder.getView<ImageView>(R.id.iv_hourse).loadImgwithloading(collectionBean.json_content.thumb)
                baseViewHolder.setText(R.id.tv_location, collectionBean.json_content.region + "(" + collectionBean.json_content.address + ")")
                baseViewHolder.setText(R.id.tv_name, collectionBean.json_content.name)
                baseViewHolder.setText(R.id.area, collectionBean.json_content.area)
                baseViewHolder.setText(R.id.tv_watch, collectionBean.json_content.visit)
                baseViewHolder.setText(R.id.tv_price, collectionBean.json_content.price)
                baseViewHolder.setText(R.id.tv_source, collectionBean.json_content.sources)
                val tagFlowLayout = baseViewHolder.getView<TagFlowLayout>(R.id.tag_flow)
                tagFlowLayout.adapter = object : com.zhy.view.flowlayout.TagAdapter<HomeBean.HouseTagsEntity>(collectionBean.json_content.house_tags) {

                    override fun getView(parent: FlowLayout, position: Int, houseTagsEntity: HomeBean.HouseTagsEntity): View {
                        val tv = View.inflate(mContext, R.layout.item_tag_flow, null) as TextView
                        tv.background = Apputils.Utils.getBackgroundDrawable(parseColor(houseTagsEntity.getColor()), parseColor(houseTagsEntity.getColor()), 1, 10.toFloat())
                        tv.setText(houseTagsEntity.getTag_name())
                        return tv
                    }
                }
            }
            2 -> {
                //                二手房
                baseViewHolder.setText(R.id.tv_source, collectionBean.json_content.sources)
                baseViewHolder.setText(R.id.tv_name, collectionBean.json_content.name)
                baseViewHolder.setText(R.id.tv_price_count, collectionBean.json_content.total_price)
                baseViewHolder.setText(R.id.tv_price_per, collectionBean.json_content.price)
                baseViewHolder.setText(R.id.tv_watch, collectionBean.json_content.visit)
                baseViewHolder.setText(R.id.tv_location, collectionBean.json_content.layout + str + collectionBean.json_content.area + str + collectionBean.json_content.house_orien + str + collectionBean.json_content.house_name)
                baseViewHolder.getView<ImageView>(R.id.iv_hourse).loadImgwithloading(collectionBean.json_content.thumb)
                val tagFlowLayoutold = baseViewHolder.getView<TagFlowLayout>(R.id.tag_flow)
                tagFlowLayoutold.adapter = object : com.zhy.view.flowlayout.TagAdapter<HomeBean.HouseTagsEntity>(collectionBean.json_content.house_tags) {

                    override fun getView(parent: FlowLayout, position: Int, houseTagsEntity: HomeBean.HouseTagsEntity): View {
                        val tv = View.inflate(mContext, R.layout.item_tag_flow, null) as TextView
                        tv.background = Apputils.Utils.getBackgroundDrawable(parseColor(houseTagsEntity.getColor()), parseColor(houseTagsEntity.getColor()), 1, 10.toFloat())
                        tv.setText(houseTagsEntity.getTag_name())
                        return tv
                    }
                }
            }
            3 -> {
                baseViewHolder.getView<ImageView>(R.id.iv_hourse).loadImgwithloading(collectionBean.json_content.thumb)
                baseViewHolder.setText(R.id.tv_source, collectionBean.json_content.sources)
                //                baseViewHolder.setText(R.id.tv_name,hourseBean.getName());
                baseViewHolder.setText(R.id.area, collectionBean.json_content.layout + str + collectionBean.json_content.area + str + collectionBean.json_content.house_orien)
                baseViewHolder.setText(R.id.tv_location, collectionBean.json_content.region + " " + collectionBean.json_content.house_name)
                baseViewHolder.setText(R.id.tv_pricerent, collectionBean.json_content.price)
                baseViewHolder.setText(R.id.tv_watch, collectionBean.json_content.visit)
                if (collectionBean.json_content.rent_type != null) {
                    if (collectionBean.json_content.rent_type == "entire") {
                        Apputils.Utils.lefttopTextview(baseViewHolder.getView<View>(R.id.tv_name) as TextView, collectionBean.json_content.house_name, R.drawable.icon_zhegnzu)
                    } else if (collectionBean.json_content.rent_type == "share") {
                        Apputils.Utils.lefttopTextview(baseViewHolder.getView<View>(R.id.tv_name) as TextView, collectionBean.json_content.house_name, R.drawable.icon_hezu)
                    }
                } else {
                    baseViewHolder.setText(R.id.tv_name, collectionBean.json_content.name)
                }
                val tagFlowLayoutrent = baseViewHolder.getView<TagFlowLayout>(R.id.tag_flow)
                tagFlowLayoutrent.adapter = object : com.zhy.view.flowlayout.TagAdapter<HomeBean.HouseTagsEntity>(collectionBean.json_content.house_tags) {

                    override fun getView(parent: FlowLayout, position: Int, houseTagsEntity: HomeBean.HouseTagsEntity): View {
                        val tv = View.inflate(mContext, R.layout.item_tag_flow, null) as TextView
                        tv.background = Apputils.Utils.getBackgroundDrawable(parseColor("#ffffff"), parseColor(houseTagsEntity.getColor()), 1, 10.toFloat())
                        tv.setTextColor(parseColor(houseTagsEntity.getColor()))
                        tv.setText(houseTagsEntity.getTag_name())
                        return tv
                    }
                }
            }
            0 -> {
                if (!TextUtils.isEmpty(collectionBean.json_content.thumb)){
                    baseViewHolder.getView<ImageView>(R.id.iv_img).visibility=View.VISIBLE
                    baseViewHolder.getView<ImageView>(R.id.iv_img).loadImgwithloading(collectionBean.json_content.thumb)
                }else{
                    baseViewHolder.getView<ImageView>(R.id.iv_img).visibility=View.GONE
                }
                baseViewHolder.setText(R.id.tv_title, collectionBean.json_content.title)
                baseViewHolder.setText(R.id.tv_time, collectionBean.create_time)
            }
        }


    }

    interface CancelListener {
        fun cancelsub(collectionBean: CollectionBean)
    }

    private fun parseColor(color: String): Int {
        return Color.parseColor(color)
    }
}
